package com.atguigu.tingshu.payment.service.impl;

import com.atguigu.tingshu.payment.config.WxPayV3Config;
import com.atguigu.tingshu.payment.context.PaymentStrategyContext;
import com.atguigu.tingshu.payment.service.PaymentInfoService;
import com.atguigu.tingshu.payment.service.WxPayService;
import com.atguigu.tingshu.payment.strategy.PaymentStrategy;
import com.atguigu.tingshu.payment.util.PayUtil;
import com.atguigu.tingshu.vo.payment.WeChatPayVo;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.core.notification.NotificationParser;
import com.wechat.pay.java.core.notification.RequestParam;
import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension;
import com.wechat.pay.java.service.payments.jsapi.model.QueryOrderByOutTradeNoRequest;
import com.wechat.pay.java.service.payments.model.Transaction;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class WxPayServiceImpl implements WxPayService {

	@Autowired
	private PaymentInfoService paymentInfoService;

	@Autowired
	private RSAAutoCertificateConfig rsaAutoCertificateConfig ;

	@Autowired
	private WxPayV3Config wxPayV3Config ;

	@Override
	public WeChatPayVo createJsapi(String paymentType, String orderNo) {
		PaymentStrategy paymentStrategy = PaymentStrategyContext.getPaymentStrategy(paymentType);
		return paymentStrategy.createJsapi(paymentType, orderNo);
	}

	@Override
	public Boolean queryPayStatus(String orderNo) {
		try {

			// 请求微信支付平台的查询支付订单的支付状态的即可
			JsapiServiceExtension service = new JsapiServiceExtension.Builder().config(rsaAutoCertificateConfig).build();
			QueryOrderByOutTradeNoRequest queryOrderByOutTradeNoRequest = new QueryOrderByOutTradeNoRequest() ;
			queryOrderByOutTradeNoRequest.setOutTradeNo(orderNo);
			queryOrderByOutTradeNoRequest.setMchid(wxPayV3Config.getMerchantId());
			Transaction transaction = service.queryOrderByOutTradeNo(queryOrderByOutTradeNoRequest);

			// 从Transaction对象中获取结果数据
			if(transaction != null) {
				Transaction.TradeStateEnum tradeState = transaction.getTradeState();
				if(tradeState == Transaction.TradeStateEnum.SUCCESS) {		//  支付成功了
					paymentInfoService.updatePaymentStatus(transaction) ;		// 更改支付信息中的支付状态
					return true ;
				}
			}

		}catch (Exception e) {
			e.printStackTrace();
			return false ;
		}

		return false;
	}

	@Override
	public void notifyOrderStatus(HttpServletRequest httpServletRequest) {
		// 从请求头中获取数据，构建RequestParam对象
		String wechatSignature = httpServletRequest.getHeader("Wechatpay-Signature");
		String wechatPaySerial = httpServletRequest.getHeader("Wechatpay-Serial");
		String wechatpayNonce = httpServletRequest.getHeader("Wechatpay-Nonce");
		String wechatTimestamp = httpServletRequest.getHeader("Wechatpay-Timestamp");
		String requestBody = PayUtil.readData(httpServletRequest);

		// 创建
		RequestParam requestParam = new RequestParam.Builder()
				.serialNumber(wechatPaySerial)
				.nonce(wechatpayNonce)
				.signature(wechatSignature)
				.timestamp(wechatTimestamp)
				.body(requestBody)
				.build();

		// 从HttpServletRequest对象中获取相关的数据进行解析
		NotificationParser parser = new NotificationParser(rsaAutoCertificateConfig);
		Transaction transaction = parser.parse(requestParam, Transaction.class);

		// 从Transaction对象中获取结果数据
		if(transaction != null) {
			Transaction.TradeStateEnum tradeState = transaction.getTradeState();
			if(tradeState == Transaction.TradeStateEnum.SUCCESS) {		//  支付成功了
				log.info("WxPayServiceImpl...notifyOrderStatus...执行了...");
				paymentInfoService.updatePaymentStatus(transaction) ;	// 更改支付信息中的支付状态
			}
		}
	}
}
